Make layout private to targets
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 8 Jul 2017 16:11:36 +0000 (19:11 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 8 Jul 2017 16:11:36 +0000 (19:11 +0300)
src/cargo/util/toml/mod.rs
src/cargo/util/toml/targets.rs

index e96bed4aef3a8e98def3cf584e8384597958d7cc..e2c7d08f96eff28e33fce5aab5efb8cc026f62fe 100644 (file)
@@ -22,7 +22,7 @@ use util::{self, ToUrl, Config};
 use util::errors::{CargoError, CargoResult, CargoResultExt};
 
 mod targets;
-use self::targets::{Layout, targets};
+use self::targets::targets;
 
 pub fn read_manifest(path: &Path, source_id: &SourceId, config: &Config)
                      -> CargoResult<(EitherManifest, Vec<PathBuf>)> {
@@ -39,8 +39,7 @@ fn do_read_manifest(contents: &str,
                     source_id: &SourceId,
                     config: &Config)
                     -> CargoResult<(EitherManifest, Vec<PathBuf>)> {
-    let root = manifest_file.parent().unwrap();
-    let layout = Layout::from_project_path(root);
+    let project_root = manifest_file.parent().unwrap();
 
     let toml = {
         let pretty_filename = util::without_prefix(manifest_file, config.cwd()).unwrap_or(manifest_file);
@@ -57,7 +56,7 @@ fn do_read_manifest(contents: &str,
     let manifest = Rc::new(manifest);
     return match TomlManifest::to_real_manifest(&manifest,
                                                 source_id,
-                                                &layout,
+                                                project_root,
                                                 config) {
         Ok((mut manifest, paths)) => {
             for key in unused {
@@ -73,7 +72,7 @@ fn do_read_manifest(contents: &str,
         Err(e) => {
             match TomlManifest::to_virtual_manifest(&manifest,
                                                     source_id,
-                                                    &root,
+                                                    project_root,
                                                     config) {
                 Ok((m, paths)) => Ok((EitherManifest::Virtual(m), paths)),
                 Err(..) => Err(e),
@@ -496,7 +495,7 @@ impl TomlManifest {
 
     fn to_real_manifest(me: &Rc<TomlManifest>,
                         source_id: &SourceId,
-                        layout: &Layout,
+                        project_root: &Path,
                         config: &Config)
                         -> CargoResult<(Manifest, Vec<PathBuf>)> {
         let mut nested_paths = vec![];
@@ -517,13 +516,13 @@ impl TomlManifest {
         // If we have no lib at all, use the inferred lib if available
         // If we have a lib with a path, we're done
         // If we have a lib with no path, use the inferred lib or_else package name
-        let targets = targets(me, layout, project_name, &project.build)?;
+        let targets = targets(me, project_root, project_name, &project.build)?;
 
         if targets.is_empty() {
             debug!("manifest has no build targets");
         }
 
-        if let Err(e) = unique_build_targets(&targets, layout) {
+        if let Err(e) = unique_build_targets(&targets, project_root) {
             warnings.push(format!("file found to be present in multiple \
                                    build targets: {}", e));
         }
@@ -541,7 +540,7 @@ impl TomlManifest {
                 config: config,
                 warnings: &mut warnings,
                 platform: None,
-                root: &layout.root,
+                root: project_root,
             };
 
             fn process_dependencies(
@@ -773,9 +772,9 @@ impl TomlManifest {
 
 /// Will check a list of build targets, and make sure the target names are unique within a vector.
 /// If not, the name of the offending build target is returned.
-fn unique_build_targets(targets: &[Target], layout: &Layout) -> Result<(), String> {
+fn unique_build_targets(targets: &[Target], project_root: &Path) -> Result<(), String> {
     let mut seen = HashSet::new();
-    for v in targets.iter().map(|e| layout.root.join(e.src_path())) {
+    for v in targets.iter().map(|e| project_root.join(e.src_path())) {
         if !seen.insert(v.clone()) {
             return Err(v.display().to_string());
         }
@@ -894,6 +893,7 @@ impl TomlDependency {
 }
 
 #[derive(Default, Serialize, Deserialize, Debug, Clone)]
+pub
 struct TomlTarget {
     name: Option<String>,
 
index 3e9f03ef06e31e5bedc52242843ed782ec86e33b..588b2d6761788a6711a05b8abee3e8e4572659a2 100644 (file)
@@ -21,13 +21,13 @@ use super::{TomlTarget, LibKind, PathValue, TomlManifest, StringOrBool,
 
 
 /// Implicit Cargo targets, defined by conventions.
-pub struct Layout {
-    pub root: PathBuf,
-    pub lib: Option<PathBuf>,
-    pub bins: Vec<PathBuf>,
-    pub examples: Vec<PathBuf>,
-    pub tests: Vec<PathBuf>,
-    pub benches: Vec<PathBuf>,
+struct Layout {
+    root: PathBuf,
+    lib: Option<PathBuf>,
+    bins: Vec<PathBuf>,
+    examples: Vec<PathBuf>,
+    tests: Vec<PathBuf>,
+    benches: Vec<PathBuf>,
 }
 
 impl Layout {
@@ -109,7 +109,8 @@ fn try_add_files(files: &mut Vec<PathBuf>, root: PathBuf) {
     /* else just don't add anything if the directory doesn't exist, etc. */
 }
 
-pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_build: &Option<StringOrBool>) -> CargoResult<Vec<Target>> {
+pub fn targets(me: &TomlManifest, project_root: &Path, project_name: &str, custom_build: &Option<StringOrBool>) -> CargoResult<Vec<Target>> {
+    let layout = Layout::from_project_path(project_root);
     let lib = match me.lib {
         Some(ref lib) => {
             lib.validate_library_name()?;
@@ -124,7 +125,7 @@ pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_bu
                 }
             )
         }
-        None => inferred_lib_target(project_name, layout),
+        None => inferred_lib_target(project_name, &layout),
     };
 
     let bins = match me.bin {
@@ -134,7 +135,7 @@ pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_bu
             };
             bins.clone()
         }
-        None => inferred_bin_targets(project_name, layout)
+        None => inferred_bin_targets(project_name, &layout)
     };
 
     for bin in bins.iter() {
@@ -151,7 +152,7 @@ pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_bu
             }
             examples.clone()
         }
-        None => inferred_example_targets(layout)
+        None => inferred_example_targets(&layout)
     };
 
     let tests = match me.test {
@@ -161,7 +162,7 @@ pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_bu
             }
             tests.clone()
         }
-        None => inferred_test_targets(layout)
+        None => inferred_test_targets(&layout)
     };
 
     let benches = match me.bench {
@@ -171,7 +172,7 @@ pub fn targets(me: &TomlManifest, layout: &Layout, project_name: &str, custom_bu
             }
             benches.clone()
         }
-        None => inferred_bench_targets(layout)
+        None => inferred_bench_targets(&layout)
     };
 
     if let Err(e) = unique_names_in_targets(&bins) {